package cn.hr.services.impl;

import java.util.List;

import javax.annotation.Resource;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.hr.dao.CommonDao;
import cn.hr.entity.College;
import cn.hr.services.ICollegeService;
import cn.hr.vo.PageAttribute;


@Service("collegeServiceImpl")
public  class CollegeServiceImpl implements ICollegeService {

	@Resource(name="commonDao")
	public CommonDao commonDao;

	public CommonDao getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	public String addCollege(String collegeName) {
		String message = "";
		if (collegeName.length() == 0)
			return message = "学院名称不能为空!";
		else {
			try {
				Criterion[] criterions = { Restrictions.eq("collegeName", collegeName) };
				List list = commonDao.find(College.class, criterions);
				if(list.size()!=0)
					return message = "学院名称已经存在!";
				College college=new College();
				college.setCollegeName(collegeName);
				commonDao.save(college);
				message = "添加学院成功!";
			} catch (Exception e) {
				e.printStackTrace();
				message = "添加学院失败!";
			}
		}
		return message;
	}

	public String deleteCollege(int collegeId) {
		String message="";
		try {
			commonDao.delete(College.class, collegeId);
			message="删除学院成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message="删除学院失败!";
		}
		return message;
	}
	
	public String modifyCollege(int collegeId,String collegeName){
		String message="";
		try {
			College college = (College)commonDao.get(College.class,collegeId);
			college.setCollegeName(collegeName);
			commonDao.update(college);
			message="修改学院成功!";
		} catch (Exception e) {
			e.printStackTrace();
			message="修改学院失败";
		}
		return message;
	}
	
	public PageAttribute findAllCollege(int page,int size)
	{
		PageAttribute pageAttribute = new PageAttribute ();
		try {
			pageAttribute.setPageList(commonDao.find(College.class, page, size));
			pageAttribute.setAllCount(commonDao.count(College.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageAttribute;
	}
}
